﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ingebet.Model;
using Ingebet.Model.Domain;
using Ingebet.Utils;

namespace Ingebet.Web
{
    public partial class AdminCompte : BasePage
    {
        protected void Page_Load(object sender , EventArgs e)
        {
            if (!IsPostBack)
            {
                RefreshDataGrid();
                string url = ConfigurationManager.AppSettings["FluxRss"];
                var lst = XmlHelper.XmlToListMatch(url);
                this.GridRencontresImportes.DataSource = lst;
                this.GridRencontresImportes.DataBind();
            }
        }

        protected void Menu_MenuItemClick(object sender , System.Web.UI.WebControls.MenuEventArgs e)
        {
            int val = Int32.Parse(e.Item.Value);
            this.MultiView.ActiveViewIndex = val;
        }

        protected void btnSupprimerClick(object sender , EventArgs e)
        {
            Button btn = (Button)sender;
            int idUtil;
            if (int.TryParse(btn.CommandArgument , out idUtil))
            {
                var util = Global.Container.Resolve<UtilisateurModel>().GetByID(idUtil);
                Global.Container.Resolve<UtilisateurModel>().Delete(util);
            }
            RefreshDataGrid();
        }

        private void RefreshDataGrid()
        {
            var lstComptes = Global.Container.Resolve<UtilisateurModel>().GetAll();
            this.GridComptes.DataSource = lstComptes;
            this.GridComptes.DataBind();

            var lstRencSansResult = Global.Container.Resolve<RencontreModel>().GetAllVueRencontreSansResult();
            this.GridRencontresEnregistres.DataSource = lstRencSansResult;
            this.GridRencontresEnregistres.DataBind();

            var lstRencAvecResult = Global.Container.Resolve<RencontreModel>().GetAllVueRencontreAvecResult();
            this.GridRencontreResult.DataSource = lstRencAvecResult;
            this.GridRencontreResult.DataBind();
        }

        protected void btnModifierClick(object sender , EventArgs e)
        {
            Button btn = (Button)sender;
            string idUtil = btn.CommandArgument;
            Page.ClientScript.RegisterClientScriptBlock(GetType() ,
                                                        "Modification comptes" ,
                                                        string.Format("window.showModalDialog('{0}','','menubar=no, center=yes, dialogtop=top, status=no, scrollbars=no, width=500,height=300');" ,
                                                                    "ModificationComptes.aspx?UtilId=" + idUtil) ,
                                                        true);
        }

        protected void GridRencontresImportes_ItemCommand(object source , DataGridCommandEventArgs e)
        {
            if (e.CommandName == "select")
            {
                int index = e.Item.ItemIndex;
                string compet = GridRencontresImportes.Items[index].Cells[0].Text;
                string equip1 = GridRencontresImportes.Items[index].Cells[1].Text;
                string equip2 = GridRencontresImportes.Items[index].Cells[2].Text;
                DateTime date = Convert.ToDateTime(GridRencontresImportes.Items[index].Cells[3].Text);
                Page.ClientScript.RegisterClientScriptBlock(GetType() ,
                                            "Modification comptes" ,
                                            string.Format("window.showModalDialog('{0}','','menubar=no, center=yes, dialogtop=top, status=no, scrollbars=no, width=800,height=500');" ,
                                                            "PageRencontre.aspx?compet=" + compet + "&equipe1=" + equip1 + "&equipe2=" + equip2 + "&date=" + date) ,
                                            true);
            }
        }

        protected void GridRencontresEnregistres_ItemCommand(object source , DataGridCommandEventArgs e)
        {
            int index = e.Item.ItemIndex;
            int renID = Convert.ToInt32(this.GridRencontresEnregistres.Items[index].Cells[0].Text);

            if (e.CommandName == "select")
            {
                Session["RencontreID"] = renID.ToString();
                string compet = this.GridRencontresEnregistres.Items[index].Cells[1].Text;
                string equip1 = this.GridRencontresEnregistres.Items[index].Cells[2].Text;
                string equip2 = this.GridRencontresEnregistres.Items[index].Cells[3].Text;
                DateTime date = Convert.ToDateTime(GridRencontresEnregistres.Items[index].Cells[4].Text);
                Page.ClientScript.RegisterClientScriptBlock(GetType() ,
                                            "Modification comptes" ,
                                            string.Format("window.showModalDialog('{0}','','menubar=no, center=yes, dialogtop=top, status=no, scrollbars=no, width=800,height=500');" ,
                                                            "Resultat.aspx") ,
                                            true);
            }
        }

        protected void GridComptes_PageIndexChanged(object source , DataGridPageChangedEventArgs e)
        {
            GridComptes.CurrentPageIndex = e.NewPageIndex;
            RefreshDataGrid();
        }

        protected void GridRencontresImportes_PageIndexChanged(object source , DataGridPageChangedEventArgs e)
        {
            GridRencontresImportes.CurrentPageIndex = e.NewPageIndex;
        }

        protected void GridRencontresEnregistres_PageIndexChanged(object source , DataGridPageChangedEventArgs e)
        {
            GridRencontresEnregistres.CurrentPageIndex = e.NewPageIndex;
        }
    }
}